/*
 * Copyright 2016-2019 yoara
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package yhao.infra.web.interceptor;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.AsyncHandlerInterceptor;
import yhao.infra.web.common.util.CommonWebUtil;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.annotation.Annotation;

/**
 * 拦截器父类，提供一些常用方法
 *
 * @author yoara
 */
public class ZBaseInterceptorAdapter implements AsyncHandlerInterceptor {
    public String[] getIpAddr(HttpServletRequest request) {
        return CommonWebUtil.getIpAddr(request);
    }
    public String getString(String name) {
        return getString(name, null);
    }
    public String getString(String name, String defaultValue) {
        return CommonWebUtil.getString(name,defaultValue);
    }
    /**
     * 如果name是1 就返回true,否则返回false
     *
     * @param name
     * @param defaultValue
     * @return
     */
    public Boolean getBoolean(String name, boolean defaultValue) {
        return CommonWebUtil.getBoolean(name,defaultValue);
    }

    /**提示提醒**/
    protected void doDealAlert(HttpServletResponse response,String message) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println("<script>alert('"+message+"');window.history.go(-1)</script>");
        out.flush();
        out.close();
    }

    /**
     * 获得所需要的注解
     * @param handler 方法handler
     * @param clazz 枚举类型
     * @param methodFirst 是否以方法注解为优先
     * @return
     */
    protected <T extends Annotation> T getAnnotation(
            Object handler, Class<T> clazz, boolean methodFirst){
        //仅校验方法级别的Handler
        if(!(handler instanceof HandlerMethod)){
            return null;
        }
        HandlerMethod handlerMethod = (HandlerMethod)handler;
        T annotationMethod = null;
        if(methodFirst){
            annotationMethod = handlerMethod.getMethod().getAnnotation(clazz);
        }
        if(annotationMethod!=null){
            return annotationMethod;
        }
        T annotationClazz = handlerMethod.getMethod().getDeclaringClass().getAnnotation(clazz);
        return annotationClazz;
    }
    /**
     * 获得所需要的注解，默认方法级枚举优先
     * @param handler 方法handler
     * @param clazz 枚举类型
     * @return
     */
    protected <T extends Annotation> T getAnnotation(Object handler,Class<T> clazz){
        return getAnnotation(handler,clazz,true);
    }
}
